<?php
	
	function editImage($img,$w,$h,$f) {
	
		$id 		= $img;
		$x			= $w;
		$y  		= $h;
		$color 		= $f;
	
		$px = $x;
		$py = $y;
		$wcolor = (empty($color)) ? ("#FFFFFF") : ($color);
		
		$fichero = strtolower($id);

		$extension = preg_split('/\./', $id, -1, PREG_SPLIT_OFFSET_CAPTURE);

		$extension = $extension[count($extension)-1][0];
		
		switch($extension){
			case "jpg":
									$imori = imagecreatefromjpeg($id);
									break;
			case "gif": 
									$imori = imagecreatefromgif($id);
									break;
			case "png": 
									$imori = imagecreatefrompng($id);
									break;
			default:
									$imori = imagecreatefromjpeg($id);
									break;
			
		}
		
	  $anc = imagesx($imori);
	  $alt = imagesy($imori);
	
	  $ox = 0;
	  $oy = 0;
	  if ($px && $py) {
		 if ($alt / ($anc / $px) <= $py) {
			$px = $px;
			$py = $alt / ($anc / $px);
		 } else {
			$px = $anc / ($alt / $py);
			$py = $py;
		 }
		 $wdx = $x;
		 $wdy = $y;
		 $ox = ($x - $px) / 2;
		 $oy = ($y - $py) / 2;
	  } elseif ($px) {
		 $px = $px;
		 $py = $px*$alt/$anc;
		 $wdx = $px;
		 $wdy = $py;
	  } elseif ($py) {
		 $px = $py*$anc/$alt;
		 $py = $py;
		 $wdx = $px;
		 $wdy = $py;
	  } else {
		 $px = $anc;
		 $py = $alt;
		 $wdx = $px;
		 $wdy = $py;
	  }
	
	  $imdes = imagecreatetruecolor($wdx, $wdy);
	
	  $off = (substr($wcolor, 0, 1) == "#") ? (1) : (0);
	
	  $wr = hexdec(substr($wcolor, 0 + $off, 2));
	  $wg = hexdec(substr($wcolor, 2 + $off, 2));
	  $wb = hexdec(substr($wcolor, 4 + $off, 2));
	
	  $wcol = ImageColorAllocate($imdes, $wr, $wg, $wb);
	  imagefill($imdes, 0, 0, $wcol);
	
	  imagecopyresampled($imdes, $imori, $ox, $oy, 0, 0, $px, $py, $anc, $alt);
	
	
		switch($extension){
			
			case "jpg":
									imagejpeg($imdes, $id, 90); 
									break;
			case "gif": 
									imagegif($imdes, $id, 90); 
									break;
			case "png": 
									imagepng($imdes, $id, 90); 
									break;
			default:
									imagejpeg($imdes, $id, 90); 
									break;
			
		}

	  imagedestroy($imori);
	  imagedestroy($imdes);	
  
	}

?>